﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYConnectBatch : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException("commandData", "is null");
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				IList<Pipe> list = new List<Pipe>();
				ISelectionFilter selectionFilter = new PipeSelectionFilter();
				HYPipeConnect hypipeConnect = new HYPipeConnect();
				IList<Element> list2 = new List<Element>();
				List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(2);
				if (!YJKRevitTools.ViewCheck(activeUIDocument, viewTypes))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					string text = "批量连接";
					int num = 0;
					transaction = new Transaction(document, text);
					transaction.Start();
					for (;;)
					{
						SubTransaction subTransaction = new SubTransaction(document);
						try
						{
							if (num != 0)
							{
								RevitVersionFuncs.ClearSelection(selection);
								list2.Clear();
								list.Clear();
							}
							num++;
							if (selection.GetElementIds().Any<ElementId>())
							{
								IEnumerator<ElementId> enumerator = selection.GetElementIds().GetEnumerator();
								{
									while (enumerator.MoveNext())
									{
										ElementId elementId = enumerator.Current;
										list2.Add(document.GetElementById(elementId));
									}
									goto IL_FB;
								}
							}
							list2 = selection.PickElementsByRectangle(selectionFilter, "请框选所要连接的水管");
							IL_FB:
							foreach (Element element in list2)
							{
								if (element is Pipe)
								{
									list.Add(element as Pipe);
								}
							}
							subTransaction.Start();
							if (list != null && list.Count >= 2)
							{
								if (!hypipeConnect.NewBeatchFitting(ref list))
								{
									MessageBox.Show("连接失败，可能原因是管道的系统类型不同", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
								}
								else
								{
									document.Regenerate();
								}
							}
							subTransaction.Commit();
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
							result = Autodesk.Revit.UI.Result.Succeeded;
							break;
						}
						catch (Exception ex)
						{
							MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
						}
						finally
						{
							if (subTransaction != null)
							{
								TransactionStatus status = subTransaction.GetStatus();
								if (status == (TransactionStatus)1)
								{
									subTransaction.Commit();
								}
								else if (status == (TransactionStatus)5)
								{
									subTransaction.RollBack();
								}
							}
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex2)
			{
				message = ex2.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status2 = transaction.GetStatus();
					if (status2 == (TransactionStatus)1)
					{
						transaction.Commit();
					}
					else if (status2 == (TransactionStatus)5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}
	}
}
